package com.weblog.follow.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weblog.follow.client.UserClient;
import com.weblog.follow.entity.Focus;
import com.weblog.follow.mapper.FocusMapper;
import com.weblog.model.user.vo.FansInfoVo;
import entity.PageResult;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author MaoLin Wang
 * @date 2019/10/3118:06
 */
@Service
public class FocusService {
    @Autowired
    private FocusMapper focusMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private UserClient userClient;

    /**
     * 粉丝列表
     *
     * @param id 当前用户id
     */
    public PageResult<List<FansInfoVo>> fansList(String id, int page, int size) {
        List<Map<String,List<String>>>list=new ArrayList<Map<String,List<String>>>();

        return handleFansAndFocus(id,page,size,1);
    }

    /**
     * 关注列表
     *
     * @param id
     */
    public PageResult<List<FansInfoVo>> focusList(String id, int page, int size) {
        return handleFansAndFocus(id, page, size, 2);
    }

    /**
     * @param id 当前用户id
     * @param page 当前页
     * @param size 页大小
     * @param type 类型 1 粉丝 其他 关注
     */
    private PageResult<List<FansInfoVo>> handleFansAndFocus(String id, int page, int size, int type) {
        if ("".equals(id) || id == null) {
            id = (String) request.getAttribute("id");
        }
        if (page < 1) {
            page = 1;
        }
        if (size < 1) {
            size = 10;
        }

        PageHelper.startPage(page, size);


        List<Focus> focuses;
        List<String> iseachList;

        if (type == 1) {
            //粉丝列表
            focuses = focusMapper.fansList(id);
            iseachList = new ArrayList<>();
            List<String> ids=new ArrayList<>();
            for (Focus f : focuses) {
                ids.add(f.getUserid());
                iseachList.add(f.getIseach());
            }

            PageInfo<Focus> pageInfo = new PageInfo<>(focuses);


            Result result = userClient.fansList(ids, iseachList);
            if (result.getData() == null) {
                return null;
            }
            return new PageResult<>(pageInfo.getPages(), (List<List<FansInfoVo>>) result.getData());
        } else {
            //关注列表
            List<String> ids=new ArrayList<>();

            focuses = focusMapper.focusList(id);
            for (Focus f : focuses){
                ids.add(f.getTargetid());
            }
            PageInfo<Focus> pageInfo = new PageInfo<>(focuses);
            Result result = userClient.focusList(ids);
            if (result.getData() == null) {
                return null;
            }
            return new PageResult<>(pageInfo.getPages(),  (List<List<FansInfoVo>>) result.getData());

        }


    }
}
